package cn.kgc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.kgc.entity.Emp;
import cn.kgc.service.EmpService;

@Controller
public class EmpController {

	@Autowired
	private EmpService empService;
	
	@RequestMapping(path = "/emp", method = RequestMethod.GET)
	public String getAllEmp(Model model){
		List<Emp> list = empService.getAllEmp();
		
		model.addAttribute("empList",list);
		
		return "empList";
	}
	
	@RequestMapping(path="/emp/add", method = RequestMethod.GET)
	public String toEmpUI(Model model) {
		model.addAttribute("emp", new Emp());
		return "emp";
	}
	
	@RequestMapping(path="/emp", method = RequestMethod.POST)
	public String addEmp(Emp emp) {
		empService.addEmp(emp);
		return "redirect:/emp";
	}
	
	@RequestMapping(path="/emp/{id}", method = RequestMethod.GET)
	public String getEmpById(@PathVariable("id") Integer id,Model model) {
		Emp emp = empService.getEmpById(id);
		model.addAttribute("emp",emp);
		return "emp";
	}
	@RequestMapping(path="/emp", method = RequestMethod.PUT)
	public String updeteEmp(Emp emp) {
		empService.updeteEmp(emp);
		return "redirect:/emp";
	}
	@RequestMapping(path="/emp/{id}", method = RequestMethod.DELETE)
	public String deleteEmpById(@PathVariable("id") Integer id) {
		empService.deleteEmpById(id);
		return "redirect:/emp";
	}
	
	
	
}
